﻿@page "/categories/{id}"
@page "/categories/{id}/pages/{pageNumber:int}"
@using AcBlog.Data.Models.Builders
@inherits BaseCategoryPage
@inject AcBlog.Services.Filters.PostCategoryFilter Categories

<PageContainer Title="@Category.OneNameOrDefault()" PageTitle="@Title">
    <Breadcrumb>
        <Breadcrumb>
            <BreadcrumbItem>
                <a href="">
                    <MdiIcon Type="@IconNames.Home" />
                    <span>Home</span>
                </a>
            </BreadcrumbItem>
            <BreadcrumbItem>
                <a href="@UrlGenerator.Categories()">
                    <MdiIcon Type="@IconNames.Category" />
                    <span>Categories</span>
                </a>
            </BreadcrumbItem>
            @for (int i = 0; i < Category.Items.Count; i++)
            {
                string name = Category.Items[i];
                @if (i < Category.Items.Count - 1)
                {
                    CategoryBuilder builder = new CategoryBuilder();
                    builder.AddSubCategory(Category.Items.Take(i + 1).ToArray());
                    <BreadcrumbItem>
                        <a href="@UrlGenerator.Category(builder.Build())">
                            <span>@name</span>
                        </a>
                    </BreadcrumbItem>
                }
                else
                {
                    <BreadcrumbItem>
                        <span>@name</span>
                    </BreadcrumbItem>
                }
            }
        </Breadcrumb>
    </Breadcrumb>
    <ChildContent>
        <Loader OnLoad="@OnLoad" LoadKey="@($"{Id}-{PageNumber}")" @ref="loader">
            <PagingList Data="@Data" Column="1" OnUpdatePage="@OnUpdatePage" OnPageTo="@OnPageTo" PageNavigationUrl="@PageNavigationUrl">
                <ItemTemplate>
                    <PostListItem Id="@context" ShowType="true"></PostListItem>
                </ItemTemplate>
            </PagingList>
        </Loader>
    </ChildContent>
</PageContainer>

@code {
    const int PageSize = 10;

    PagingData<string> Data { get; set; }

    [Parameter]
    public string Id { get; set; }

    [Parameter]
    public int? PageNumber { get; set; } = null;

    private Category Category = Category.Empty;

    private Loader loader;

    private string PageNavigationUrl;

    private async Task OnLoad()
    {
        Category = AcBlog.Data.Models.Category.Parse(Id);
        PageNavigationUrl = UrlGenerator.Category(Category);
        Title = "Loading...";
        Data = await Categories.Filter(Category, new Data.Models.Actions.Pagination
        {
            CurrentPage = PageNumber ?? 0,
            PageSize = PageSize
        }, QueryTimeOrder.CreationTimeDescending);
        Title = $"Page {Data.CurrentPage.CurrentPage + 1} - {Category.ToString()}";
    }

    private async Task OnPageTo(int pageNumber)
    {
        PageNumber = pageNumber;
        await OnUpdatePage();
    }

    private async Task OnUpdatePage() => await loader.Load();
}
